package com.sannuo.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sannuo.common.domain.MedicalReport;
import com.sannuo.common.domain.vo.MedicalReportVo;
import com.sannuo.common.result.Result;
import com.sannuo.common.result.ResultCode;
import com.sannuo.system.service.MedicalReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(value = "/zxt")
@RestController
@Api(tags = "MedicalReport" ,description = "赵心涛")
@CrossOrigin
public class MedicalReportController {

    @Autowired
    private MedicalReportService medicalReportService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有用户信息",notes = "查询所有用户信息",httpMethod = "GET")
    public Result fin(Integer page, Integer limit, String searchParams) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        MedicalReportVo book = null;
        if (searchParams != null){
            book = objectMapper.readValue(searchParams,MedicalReportVo.class);
        }
        Page page1 = PageHelper.startPage(page,limit);
        List<MedicalReport> users = medicalReportService.selectMedicalReport(book);
        for (MedicalReport user : users) {
            user.setQj(user.getReferenceLow()+"~"+user.getReferenceHigh());
            System.out.println(user);
        }

        PageInfo pageInfo = new PageInfo<>(users);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),users);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询", notes = "根据id查询", httpMethod = "GET")
    public Result delUserByID(@PathVariable("id") Integer id) throws Exception{

        List<MedicalReport> users = medicalReportService.selectMedicalReportById(id);
        for (MedicalReport user : users) {
            user.setQj(user.getReferenceLow()+"~"+user.getReferenceHigh());
        }
        PageInfo pageInfo = new PageInfo<>(users);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),users);
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "根据id修改", notes = "根据id修改", httpMethod = "POST")
    public Result updateMedicalReportById(MedicalReport medicalReport) throws Exception{

        int i= medicalReportService.updateMedicalReportById(medicalReport);

        return Result.ok(ResultCode.SUCCESS,i);
    }


}
